package com.qf.day07.poly;

import com.qf.day06.Dog;

import java.util.Random;

/**
 * 武器工具类
 */
public class WeaponUtils {

    public static void main(String[] args) {

        Weapon[] randomWeapons = WeaponUtils.createRandomWeapons(5);
        for (int i = 0; i < randomWeapons.length; i++) {
            System.out.println(randomWeapons[i]);
        }
        System.out.println("=============================");
        // 对武器进行分类
        LongSword[] longSwords = new LongSword[randomWeapons.length];
        Dagger[] daggers = new Dagger[randomWeapons.length];
        WeaponUtils.typeWeapons(randomWeapons,longSwords,daggers);

        for (LongSword longSword : longSwords) {
            System.out.println(longSword);
        }

        for (Dagger dagger : daggers) {
            System.out.println(dagger);
        }

    }

    /**
     * 把武器按照长剑和匕首进行分类
     * @param weapons      武器数组
     * @param longSwords   长剑数组
     * @param daggers      匕首数组
     */
    public static void typeWeapons(Weapon[] weapons,LongSword[] longSwords,Dagger[] daggers){

        int longSwordIndex = 0;
        int daggerIndex = 0 ;

        for (int i = 0; i < weapons.length; i++) {

            Weapon weapon = weapons[i];
            // 判断weapon对象是不是长剑呢？
            if (weapon instanceof LongSword) {
                LongSword longSword = (LongSword) weapon;
                // 放入长剑数组中
                longSwords[longSwordIndex++]=longSword;
            }

            if (weapon instanceof Dagger){
                Dagger dagger = (Dagger) weapon;
                daggers[daggerIndex++] = dagger;
            }

        }

    }

    /**
     * 创建随机数量的武器
     * @param num
     * @return
     */
    public static Weapon[] createRandomWeapons(int num){

        // 创建用来存放武器的数组
        Weapon[] weapons = new Weapon[num];
        String[] daggerNames = {"蝴蝶刀","爪刀","萝卜刀","刺刀","橙匕"};
        String[] LongSwordNames = {"太极天地剑","流光星陨刀","无影剑","霜之哀伤","魔剑"};
        for (int j=0;j<num;j++) {
            // 创建随机的长剑或者匕首对象
            Random random = new Random();
            int i = random.nextInt(2); //[0,4]
            switch (i){
                case 0 :
                    // 创建匕首
                    Dagger dagger = new Dagger();
                    dagger.setName(daggerNames[random.nextInt(daggerNames.length)]);
                    dagger.setAtkSpeed(1+random.nextInt(10));
                    dagger.setMovSpeed(1+random.nextInt(3));
                    dagger.setDamage(1+random.nextInt(50));
                    dagger.setRange(1+random.nextInt(8));
                    dagger.setLevelLimit(1+random.nextInt(20));
                    // 把创建好的匕首放入数组中
                    weapons[j] = dagger;
                    break;
                case 1 :
                    // 创建长剑
                    LongSword longSword = new LongSword();
                    longSword.setName(LongSwordNames[random.nextInt(LongSwordNames.length)]);
                    longSword.setDamage(1+random.nextInt(50));
                    longSword.setRange(1+random.nextInt(8));
                    longSword.setLevelLimit(1+random.nextInt(20));
                    longSword.setSwordLevel(random.nextInt(10));
                    longSword.setCrit(random.nextInt(30));
                    weapons[j] = longSword;
                    break;
            }
        }
        return weapons;
    }

}
